package com.petalsgroup.petals.dao.bannedTime.impl;

import com.petalsgroup.petals.dao.bannedTime.BannedTimeDao;
import com.petalsgroup.petals.pojo.BannedTimeDO;
import com.petalsgroup.petals.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BannedTimeDaoImpl implements BannedTimeDao {
    private final JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 查看该用户是否已经被禁言
     * @param uided
     * @return
     * @author wgd
     */
    @Override
    public boolean findUserByUid(Integer uided) {
        String sql="select * from banned_time where uid = ?";
        List<BannedTimeDO> list = new ArrayList<>();
        try {
            list = template.query(sql, new BeanPropertyRowMapper<>(BannedTimeDO.class),uided);
        } catch (Exception e) {

        }
        return list.size()>0;
    }

    /**
     * 获取某个用户的解禁时间
     * @param uided
     * @return
     * @author wgd
     */
    @Override
    public Date getOpenTimeByUid(Integer uided) {
        String sql="select open_time from banned_time where uid = ?";
        Date time = template.queryForObject(sql, Date.class, uided);
        return time;
    }

    /**
     * 延迟用户的解禁时间
     * @param uided
     * @param openTime
     * @author wgd
     */
    @Override
    public void addOpenTimeByUided(Integer uided, String openTime) {
        String sql="update banned_time set open_time = ? where uid = ?";
        template.update(sql,openTime,uided);
    }

    /**
     * 为用户增加解禁记录
     * @param uided
     * @param openTime
     * @author wgd
     */
    @Override
    public void insertOpenTime(Integer uided, String openTime) {
        String sql="insert into banned_time(uid,open_time) values (?,?)";
        template.update(sql,uided,openTime);
    }
    /**
     * 获取所有用户的禁言时间
     * @return
     * @author wgd
     */
    @Override
    public List<BannedTimeDO> getAllOpenTime() {
        String sql="select * from banned_time";
        List<BannedTimeDO> list = template.query(sql, new BeanPropertyRowMapper<>(BannedTimeDO.class));
        return list;
    }

    /**
     * 删除禁言时间信息（禁言时间到了）
     * @param uid
     */
    @Override
    public void deletBannedTimeById(Integer uid) {
        System.out.println("进入了删除方法");
        String sql="delete from banned_time where uid = ?";
        template.update(sql,uid);
    };

}
